package com.gxa.clientadmin.controller;

/*
 *
 *@author leijun
 *
 *@creating 2020/11/13 16:51
 *
 */

import com.gxa.clientadmin.exception.BusinessException;
import com.gxa.clientadmin.pojo.Admin;
import com.gxa.clientadmin.pojo.Logs;
import com.gxa.clientadmin.service.AdminService;
import com.gxa.clientadmin.service.LogsService;
import com.gxa.clientadmin.utils.JWTUtils;
import com.gxa.clientadmin.utils.JsonResult;
import com.gxa.clientadmin.utils.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName AdminController
 * @Description TODO
 * @Author 俊
 * @Date 2020/11/13 16:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/adm")
public class AdminController {

    @Resource
    AdminService adminService;

    @Resource
    LogsService logsService;

    @RequiresPermissions("26")
    @PostMapping("/add")
    public Map add(@RequestParam("username") String username,@RequestParam("password") String password,@RequestParam("rePassword") String rePassword){
        JsonResult jsonResult = new JsonResult();
        if (!rePassword.trim().equals(password)){
            throw new BusinessException("两次密码不相同");
        }
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        Integer add = adminService.add(admin);
        if (add != 1){
            throw new BusinessException("添加失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getValues();
    }


    @RequiresPermissions("26")
    @PostMapping("list")
    public Map list(){
        JsonResult jsonResult = new JsonResult();
        Map list = adminService.list();
        if (list == null){
            throw new BusinessException("查询失败");
        }
        jsonResult.put("liat",list.get("list"));
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("200");
        return jsonResult.getValues();
    }

    @RequiresPermissions("26")
    @PostMapping("/del")
    public Map delete(@RequestParam("id") Integer id){
        JsonResult jsonResult = new JsonResult();
        Integer delete = adminService.delete(id);
        if (delete != 1){
            throw new BusinessException("删除失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getValues();
    }

    @RequiresPermissions("26")
    @PostMapping("/update")
    public Map update(@RequestParam("id") Integer id,@RequestParam("password") String password,@RequestParam("rePassword") String rePassword){
        JsonResult jsonResult = new JsonResult();
        if (!rePassword.trim().equals(password)){
            throw new BusinessException("两次密码不相同");
        }
        Integer update = adminService.update(id, password);
        if (update!=1){
            throw new BusinessException("修改密码失败");
        }
        jsonResult.setMsg("修改密码成功");
        jsonResult.setCode("200");
        return jsonResult.getValues();
    }

    @RequiresPermissions("26")
    @PostMapping("/allot")
    public Map allot(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        Integer integer = adminService.updatePermission(map);
        if (integer != 1){
            throw new BusinessException("添加失败");
        }
        jsonResult.setMsg("添加成功");
        jsonResult.setCode("200");

        return jsonResult.getValues();
    }

    @RequiresPermissions("27")
    @PostMapping("/log")
    public Map log(@RequestParam("id") Integer id,@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){
        JsonResult jsonResult = new JsonResult();
        Map log = logsService.log(id, page, limit);
        if (log==null){
            throw new BusinessException("查询失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.put("list",log.get("list"));
        jsonResult.put("count",log.get("count"));
        return jsonResult.getValues();
    }

    @RequiresPermissions("28")
    @PostMapping("/logs")
    public Map logs(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){
        JsonResult jsonResult = new JsonResult();
        Map logs = logsService.logs(page, limit);
        if (logs==null){
            throw new BusinessException("查询失败");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.put("list",logs.get("list"));
        jsonResult.put("count",logs.get("count"));
        return jsonResult.getValues();
    }


    @PostMapping("/login")
    public Map login(@RequestParam("username") String username,@RequestParam("password") String password){
        JsonResult jsonResult = new JsonResult();
        Admin admin = adminService.selectOne(username);
        Map login = adminService.login(username, password);
        if (login ==null){
            throw new BusinessException("用户名或密码错误");
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, MD5Util.encode(password+admin.getSalt()));
        try {
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException e) {
           throw new BusinessException("用户名不存在");
        } catch (AuthenticationException e) {
           throw new BusinessException("账号或密码错误");
        } catch (AuthorizationException e) {
            throw new BusinessException("没有权限");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("登录成功");
        jsonResult.put("admin",login.get("admin"));
        jsonResult.put("permission",login.get("permission"));
        JWTUtils jwtUtils = new JWTUtils();
        String jwt = jwtUtils.createJWT("1", admin.getUsername(), 300000);
        jsonResult.put("token",jwt);
        return jsonResult.getValues();
    }

    @PostMapping("/addlog")
    public Map addLog(@RequestBody Logs logs){
        JsonResult jsonResult = new JsonResult();
        Integer add = logsService.add(logs);
        if (add!=1){
            throw new BusinessException("添加失败");
        }
        jsonResult.setMsg("添加成功");
        jsonResult.setCode("200");
        return jsonResult.getValues();
    }
}
